import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:provider_demo/model/demo_model.dart';
import 'package:provider_demo/provide/provider_widget.dart';
import 'package:provider_demo/router/router_manager.dart';

void main() async{
  WidgetsFlutterBinding.ensureInitialized();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'ProviderDemo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      navigatorKey: Router.navigatorkey,
      debugShowCheckedModeBanner: false,
      onGenerateRoute: Router.generateRoute,
      //不配置复制粘贴会奔溃
      localizationsDelegates: [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate
      ],
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  var model = DemoModel();
  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: ProviderWidget<DemoModel>(
        model: model,
        onModelReady: (model){
               model.init();
        },
        builder: (context,model,child){

             if(model.isBusy){
               print("is busy");
             }
             else if(model.isEmpty){
               print("is empty");
             }
             else if (model.isError){
               print("is error");
             }
            return  Center(
              child: Column(

                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text(
                    'You have pushed the button this many times:',
                  ),
                  Text(
                    '${model.num}',
                    style: Theme.of(context).textTheme.headline4,
                  ),
                  GestureDetector(
                    onTap: (){
                      model.add();
                    },
                    child: Container(
                      padding: EdgeInsets.all(7),
                      color: Colors.blue,
                      child: Text("Add"),
                    ),
                  ),
                  GestureDetector(
                    onTap: (){
                      Navigator.of(context).pushNamed(RouterName.demo);
                    },
                    child: Container(
                      margin: EdgeInsets.all(7),
                      padding: EdgeInsets.all(10),
                      color: Colors.blue,
                      child: Text("示例demo"),
                    ),
                  )
                ],
              ),
            );
        },
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: model.add,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
